<?php

namespace app\stock\controller;

use think\Controller;
/*
公海
*/

class Privatesea extends Controller
{
    // 用户模型
    protected $BusinessModel;
    protected $RegionModel;
    private $AdminModel;
    // 客户来源模型
    protected $SourceModel;
    protected $relationSearch = true;
    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        $this->BusinessModel = model('Business.Business');
        $this->AdminModel = model('Admin');
        // 城市模型
        $this->RegionModel = model('Region.Region');
        $this->SourceModel = model('Business.Source');
    }
    /**
     * 私海首页
     */
    public function index()
    {
        $list = $this->BusinessModel->with('scource')->whereNotNull('adminid')->order('id', 'desc')->select();
        $this->success("", "", $list);
    }
    /**
     * 详情
     */
    public function info()
    {
        $id = $this->request->param('id', 0, 'trim');
        $info = $this->BusinessModel->with(['scource', 'district', 'city', 'province'])->find($id);
        if (!$info) {
            $this->error("用户不存在");
        }
        $this->success("", "", $info);
    }
    /**
     * 删除
     */
    public function del()
    {
        $id = $this->request->param('id', 0, 'trim');
        $info = $this->BusinessModel->find($id);
        if (!$info) {
            $this->error("用户不存在");
        }
        $result = $this->BusinessModel->destroy($id);
        if ($result == false) {
            $this->error("删除失败");
        }
        $this->success("删除成功");
    }
    /**
     * 用户添加
     */
    public function add()
    {
        $params = $this->request->param();
        $data = [
            'mobile' => $params['mobile']??'',
            'nickname' => $params['nickname']??'',
            'email' => $params['email']??'',
            'gender' => $params['gender']??0,
            'auth' => $params['auth']??0,
            'sourceid' => $params['sourceid']??0,
            'money' => $params['money']??0,
            'deal' => $params['deal']??0,
            'adminid' => $params['adminid']??0,
        ];
        $salt = randstr();
        $password = md5($params['mobile'] . $salt);
        $data['password'] = $password;
        $data['salt'] = $salt;
        $admininfo = $this->AdminModel->find($data['adminid']);
        if (!$admininfo) {
            $this->error('管理员不存在');
        }
        $source=$this->SourceModel->find($data['sourceid']);
        if (!$source) {
            $this->error("来源不存在");
        }
        if (isset($params['region']) && $params['region']) {
            $parentpath = $this->RegionModel->where('code', $params['region'])->value('parentpath');
            if ($parentpath) {
                $addcode = explode(',', $parentpath);
                $data['province'] = $addcode[0] ?? '';
                $data['city'] = $addcode[1] ?? '';
                $data['district'] = $addcode[2] ?? '';
            } else {
                $this->error("修改失败,无该地区信息");
            }
        }
        $result = $this->BusinessModel->validate('common/Business/Business')->save($data);
        if ($result == false) {
            $this->error($this->BusinessModel->getError());
        }
        $this->success("添加成功");
    }
    /**
     * 用户修改
     */
    public function edit()
    {
        $params = $this->request->param();
        $data = [
            'mobile' => $params['mobile']??'',
            'nickname' => $params['nickname']??'',
            'email' => $params['email']??'',
            'gender' => $params['gender']??0,
            'auth' => $params['auth']??0,
            'sourceid' => $params['sourceid']??0,
            'money' => $params['money']??0,
            'deal' => $params['deal']??0,
            'id'=>$params['id']??0,
        ];
        $info = $this->BusinessModel->find($params['id']);
        if (!$info) {
            $this->error("用户不存在");
        }
        $source=$this->SourceModel->find($data['sourceid']);
        if (!$source) {
            $this->error("来源不存在");
        }
        if (isset($params['region']) && $params['region']) {
            $parentpath = $this->RegionModel->where('code', $params['region'])->value('parentpath');
            if ($parentpath) {
                $addcode = explode(',', $parentpath);
                $data['province'] = $addcode[0] ?? '';
                $data['city'] = $addcode[1] ?? '';
                $data['district'] = $addcode[2] ?? '';
            } else {
                $this->error("修改失败,无该地区信息");
            }
        }
        $result = $this->BusinessModel->validate('common/Business/Business.add')->isUpdate()->save($data);
        if ($result === false) {
            $this->error($this->BusinessModel->getError());
        }
        $this->success("修改成功");
    }
    // 回收
    public function recycle(){
        $id = $this->request->param('id', 0, 'trim');
        $info = $this->BusinessModel->find($id);
        if (!$info) {
            $this->error("用户不存在");
        }
        $result = $this->BusinessModel->isUpdate()->save(['id'=>$id,'adminid'=>null]);
        if ($result == false) {
            $this->error("回收失败");
        }
        $this->success("回收成功");
    }

}
